#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string.h>

#include "main.h"

typedef enum
{
    BLACK,
    RED,
    GREEN,
    BLUE,
    YELLOW
} Color;

using std::cin;
using std::cout;
using std::endl;

int startMoney = 10000;
char choosePlayer[4]={0};
int playerNumber;
int aliveNumber;
int order[4];
int nowPlayer = 0;
int nowPosition = 0;
char choice; // y n
char command[40];
int moves = 0;
Place place[70];
Player player[5]; // 1Q 2A 3S 4J

char commands[20][20] = {'\0'};
int num_word = 0;

int set_block(int relativePosition);
void help_operations();
int command_separate();
int command_deal(int people);
void printMap();
void init();
int Roll();
void setColor(int);
void Walk(int);
void toolRoom();
void PrintState(int);
int charToInt(char *);
int setSeq(char Name);
void fine();
void setNameColumnData(char, int, int);

int char2int(char* input);
int preset_deal();
bool IsValid(char* input){
    int len = strlen(input);
    char tmp;
    bool has[4]={false,false,false,false};
    if(len!=2&&len!=3&&len!=4) return false;
    for (int i=0;i<len ;i++){
        if (input[i]<'1'||input[i]>'4'){
            return false;
        }
    }
    for(int i =0 ;i<len;i++){

        if(has[input[i]-'1'])
            return false;
        else
            has[input[i]-'1'] = true;
    }
    return true;
}

//初始化特殊数据
void init()
{
    // Special Position
    place[0].Char = 'S';
    place[0].Owner = -1;
    place[14].Char = 'P';
    place[14].Owner = 6;
    place[28].Char = 'T';
    place[28].Owner = 7;
    place[35].Char = 'G';
    place[35].Owner = 8;
    place[49].Char = 'P';
    place[49].Owner = 9;
    place[63].Char = 'M';
    place[63].Owner = 10;
    // Mine
    place[64].Char = '$';
    place[64].Owner = 5;
    place[64].count = 60;
    place[65].Char = '$';
    place[65].Owner = 5;
    place[65].count = 80;
    place[66].Char = '$';
    place[66].Owner = 5;
    place[66].count = 40;
    place[67].Char = '$';
    place[67].Owner = 5;
    place[67].count = 100;
    place[68].Char = '$';
    place[68].Owner = 5;
    place[68].count = 80;
    place[69].Char = '$';
    place[69].Owner = 5;
    place[69].count = 20;

    //地段价格
    for (int i = 1; i <= 13; i++)
    {
        place[i].price = 200;
    }
    for (int i = 15; i <= 27; i++)
    {
        place[i].price = 200;
    }
    for (int i = 29; i <= 34; i++)
    {
        place[i].price = 500;
    }
    for (int i = 36; i <= 48; i++)
    {
        place[i].price = 300;
    }
    for (int i = 50; i <= 62; i++)
    {
        place[i].price = 300;
    }

    strcpy(player[1].Name, "钱夫人");
    strcpy(player[2].Name, "阿土伯");
    strcpy(player[3].Name, "孙小美");
    strcpy(player[4].Name, "金贝贝");
}
//显示地图
void printMapChar(int pos)
{
    setColor(0);
    char c = place[pos].Char;
    if (place[pos].Owner >= 1 && place[pos].Owner <= 4) {
        c = (char)place[pos].level + 48;
        setColor(place[pos].Owner);
    }
    if (place[pos].Tool != 0) {
        c = '#';
    }
    int w = -1;
    const char name[5] = "QASJ";
    for (int i = 0; i < aliveNumber; i++) {
        if (player[order[i]].Position == pos && !player[order[i]].Bust && player[order[i]].lastMove > w) {
            c = name[order[i] - 1];
            w = player[order[i]].lastMove;
            setColor(order[i]);
        }
    }
    cout << c;
}

void printMap()
{
    for (int i = 0; i < 29; i++) {
        printMapChar(i);
    }
    for (int i = 0; i < 6; i++) {
        cout << endl;
        printMapChar(69 - i);
        for (int j = 0; j < 27; j++) {
            cout << " ";
        }
        printMapChar(29 + i);
    }
    cout << endl;
    for (int i = 63; i > 34; i--) {
        printMapChar(i);
    }
    setColor(0);
    cout << endl;
}

//随机骰子
int Roll()
{
    //随机数
    srand((unsigned)time(NULL));
    int num = rand() % 6;
    num++;
    return num;
}
//设置颜色
void setColor(int player)
{
    switch (player)
    {
        case -1:
        case 0:
            cout << "\033[30m";
            break; // normal
        case 1:
            cout << "\033[31m";
            break; // red
        case 2:
            cout << "\033[32m";
            break; // green
        case 3:
            cout << "\033[34m";
            break; // blue
        case 4:
            cout << "\033[33m";
            break; // yellow
        default:
            break;
    }
}
// Roll or step
void Walk(int num)
{
    player[nowPlayer].lastMove = ++moves;
    for (int stepOneByOne = 0; stepOneByOne < num; stepOneByOne++)
    {
        nowPosition++;
        if (nowPosition == 70)
        {
            nowPosition = 0;
        }
        //遇到道具
        if (place[nowPosition].Tool == 0)
        {
            continue;
        }
        else if (place[nowPosition].Tool == 2)
        { // block
            player[nowPlayer].Position = nowPosition;
            place[nowPosition].Tool = 0;
            cout<<"您被路障拦住了！！"<<endl;
            break;
        }
    }
    player[nowPlayer].Position = nowPosition;
    ////system("cls");
    printMap();
    setColor(nowPlayer);
    cout << "您当前的位置为："<<player[nowPlayer].Position << endl;
    //买地
    if (place[nowPosition].Owner == 0)
    {
        askBuy:
        cout << "您想购买这块空地吗？（y：是   n：否）";
        cin.clear();
        cin.sync();
        cin >> choice;
        if (choice == 'y' || choice == 'Y')
        {
            if (player[nowPlayer].money < place[player[nowPlayer].Position].price)
            {
                cout << "对不起，您的金钱不足！" << endl;
            }
            else
            {
                player[nowPlayer].money -= place[player[nowPlayer].Position].price;
                place[nowPosition].Owner = nowPlayer;
                printMap();
                // place和map的Char还是0
            }
        }
        else if (choice != 'n' && choice != 'N') {
            goto askBuy;
        }
    }
        //升级
    else if (place[nowPosition].Owner == nowPlayer)
    {
        if (place[nowPosition].level == 0)
        {
            askUpgrade1:
            cout << "您想升级这块属于您的空地吗？（y：是   n：否）";
            cin.clear();
            cin.sync();
            cin >> choice;
            if (choice == 'y' || choice == 'Y')
            {
                if (player[nowPlayer].money < place[nowPosition].price)
                {
                    cout << "对不起，您的金钱不足！" << endl;
                }
                else
                {
                    player[nowPlayer].money -= place[player[nowPlayer].Position].price;
                    place[nowPosition].level++;
                    printMap();
                }
            }
            else if (choice != 'n' && choice != 'N') {
                goto askUpgrade1;
            }
        }
        else if (place[nowPosition].level == 1)
        {
            askUpgrade2:
            cout << "您想升级这块属于您的茅屋吗？（y：是   n：否）";
            cin.clear();
            cin.sync();
            cin >> choice;
            if (choice == 'y' || choice == 'Y')
            {
                if (player[nowPlayer].money < place[nowPosition].price)
                {
                    cout << "对不起，您的金钱不足！" << endl;
                }
                else
                {
                    player[nowPlayer].money -= place[player[nowPlayer].Position].price;
                    place[nowPosition].level++;
                    printMap();
                }
            }
            else if (choice != 'n' && choice != 'N') {
                goto askUpgrade2;
            }
        }
        else if (place[nowPosition].level == 2)
        {
            askUpgrade3:
            cout << "您想升级这块属于您的洋房吗？（y：是   n：否）";
            cin.clear();
            cin.sync();
            cin >> choice;
            if (choice == 'y' || choice == 'Y')
            {
                if (player[nowPlayer].money < place[nowPosition].price)
                {
                    cout << "对不起，您的金钱不足！" << endl;
                }
                else
                {
                    player[nowPlayer].money -= place[player[nowPlayer].Position].price;
                    place[nowPosition].level++;
                    printMap();
                }
            }
            else if (choice != 'n' && choice != 'N') {
                goto askUpgrade3;
            }
        }
    }
        //过路费
    else if (place[nowPosition].Owner >= 1 && place[nowPosition].Owner <= 4)
    {
        fine();
    }
        //特殊位置
    else
    {
        switch (place[nowPosition].Owner)
        {
            case 5: // Mine
                cout << "您走到了矿地，获得" << place[nowPosition].count << "点数" << endl;
                player[nowPlayer].credit += place[nowPosition].count;
                break;
            case 6: // Hospital
                break;
            case 7: // ToolRoom
                toolRoom();
                break;
            case 8: // GiftRoom
                cout << "欢迎光临礼品屋，请选择一件您喜欢的礼品：" << endl;
                cout << "编号\t1\t2\t3" << endl;
                cout << "礼品\t奖金\t点数卡\t财神" << endl;
                cout << "请输入编号：";
                int giftChoice;
                cin.clear();
                cin.sync();
                cin >> giftChoice;
                switch (giftChoice)
                {
                    case 1:
                        player[nowPlayer].money += 2000;
                        cout << "恭喜您！获得2000金币！！" << endl;
                        break;
                    case 2:
                        player[nowPlayer].credit += 200;
                        cout << "恭喜您！获得200点！！" << endl;
                        break;
                    case 3:
                        player[nowPlayer].god = 6;
                        cout << "恭喜您！获得5轮财神！！" << endl;
                        break;
                    default:
                        cout << "输入错误！未获得礼品！！退出礼品屋！！！" << endl;
                        break;
                }
                break;
            case 9: // Prison
                break;
            case 10: // MagicRoom
                if (player[1].Position >= 0 && !player[1].Bust)
                {
                    setColor(1);
                    cout << "1. 钱夫人 ";
                }
                if (player[2].Position >= 0 && !player[2].Bust)
                {
                    setColor(2);
                    cout << "2. 阿土伯 ";
                }
                if (player[3].Position >= 0 && !player[3].Bust)
                {
                    setColor(3);
                    cout << "3. 孙小美 ";
                }
                if (player[4].Position >= 0 && !player[4].Bust)
                {
                    setColor(4);
                    cout << "4. 金贝贝 ";
                }
                setColor(nowPlayer);
                cout << endl;

                int player_num = 0;
                bool go_on = true;
                do
                {
                    cout << "请输入你想陷害的玩家编号：";
                    cin.clear();
                    cin.sync();
                    cin >> player_num;
                    if (player[player_num].Bust)
                    {
                        cout << "此玩家已破产！" << endl;
                    }
                    else if (player[player_num].Position == -1)
                    {
                        cout << "此玩家不在游戏中！" << endl;
                    }
                    else if (player_num > 0 && player_num <= 4)
                    {
                        player[player_num].state -= 2;
                        go_on = false;
                    }
                    else if (player_num != 0)
                    {
                        cout << "输入错误！" << endl;
                    }
                } while (player_num != 0 && go_on);
        }
    }
    //减财神
    if (player[nowPlayer].god > 0)
    {
        player[nowPlayer].god--;
    }
}

void fine()
{
    //财神
    if (player[nowPlayer].god > 0) {
        cout << "您是财神，免收租金" << endl;
        return;
    }
    else {
        int fine = (place[nowPosition].price / 2) * (place[nowPosition].level + 1);
        //破产
        if (fine > player[nowPlayer].money) {
            cout << "您破产了！" << endl;
            player[nowPlayer].Bust = true;
            //房产收回
            for (int checkOwner = 1; checkOwner < 65; checkOwner++) {
                if (place[checkOwner].Owner == nowPlayer) {
                    place[checkOwner].Owner = 0;
                }
            }
            aliveNumber--;
        }
            //收费
        else {
            player[nowPlayer].money -= fine;
            player[place[nowPosition].Owner].money += fine;
            cout << "您走到了其他人的土地上，交租金" << fine << "元！" << endl;
        }
    }
}

//道具屋
void toolRoom()
{
    while (player[nowPlayer].credit >= 30)
    {
        cout << "欢迎光临道具屋，请选择您所需要的道具（F：退出道具屋）" << endl;
        cout << "道具\t\t编号\t价值（点数）\t显示方式" << endl;
        cout << "路障\t\t1\t50\t#" << endl;
        cout << "机器娃娃\t2\t30\t" << endl;
        char toolChoice[20];

        InputChoice:
        cout << "请输入编号：";
        cin.clear();
        cin.sync();
        cin >> toolChoice;

        if (strcmp(toolChoice, "F") == 0 || strcmp(toolChoice, "f") == 0)
        {
            return;
        }
        if (player[nowPlayer].Block_num + player[nowPlayer].Robot_num >= 10)
        {
            cout << "对不起，您已拥有10个道具！无法购买！！" << endl;
            continue;
        }
        if (strcmp(toolChoice, "1") == 0)
        {
            if (player[nowPlayer].credit < 50)
            {
                cout << "点数不足！购买失败！！" << endl;
                continue;
            }
            player[nowPlayer].credit -= 50;
            player[nowPlayer].Block_num++;
            cout << "购买成功！ " << endl;
        }
        else if (strcmp(toolChoice, "2") == 0)
        {
            player[nowPlayer].credit -= 30;
            player[nowPlayer].Robot_num++;
            cout << "购买成功！ " << endl;
        }
        else
        {
            cout << "输入错误！" << endl;
            goto InputChoice;
        }
    }
    cout << "点数不足！" << endl;
}

int setSeq(char Name)
{
    switch (Name)
    {
        case 'q':
            return 1;
        case 'a':
            return 2;
        case 's':
            return 3;
        case 'j':
            return 4;
        default:
            return 0;
    }
}

//名字switch
void setNameColumnData(char Name, int Column, int Data)
{
    if (Name >= 'a' && Name <= 'z') {
        Name -= 32;
    }
    switch (Column)
    {
        case 1:
            switch (Name)
            {
                case 'Q':
                    player[1].Position = Data;
                    break;
                case 'A':
                    player[2].Position = Data;
                    break;
                case 'S':
                    player[3].Position = Data;
                    break;
                case 'J':
                    player[4].Position = Data;
                    break;
                default:
                    break;
            }
            break;
        case 2:
            if (Data != -1) {
                switch (Name) {
                    case 'Q':
                        player[1].money = Data;
                        break;
                    case 'A':
                        player[2].money = Data;
                        break;
                    case 'S':
                        player[3].money = Data;
                        break;
                    case 'J':
                        player[4].money = Data;
                        break;
                    default:
                        break;
                }
            } else {
                switch (Name) {
                    case 'Q':
                        player[1].Bust = true;
                        break;
                    case 'A':
                        player[2].Bust = true;
                        break;
                    case 'S':
                        player[3].Bust = true;
                        break;
                    case 'J':
                        player[4].Bust = true;
                        break;
                    default:
                        break;
                }
            }
            break;
        case 3:
            switch (Name)
            {
                case 'Q':
                    player[1].credit = Data;
                    break;
                case 'A':
                    player[2].credit = Data;
                    break;
                case 'S':
                    player[3].credit = Data;
                    break;
                case 'J':
                    player[4].credit = Data;
                    break;
                default:
                    break;
            }
            break;
        case 4:
            switch (Name)
            {
                case 'Q':
                    player[1].state = -Data;
                    break;
                case 'A':
                    player[2].state = -Data;
                    break;
                case 'S':
                    player[3].state = -Data;
                    break;
                case 'J':
                    player[4].state = -Data;
                    break;
                default:
                    break;
            }
            break;
        case 5:
            switch (Name)
            {
                case 'Q':
                    player[1].Bomb_num = Data;
                    break;
                case 'A':
                    player[2].Bomb_num = Data;
                    break;
                case 'S':
                    player[3].Bomb_num = Data;
                    break;
                case 'J':
                    player[4].Bomb_num = Data;
                    break;
                default:
                    break;
            }
            break;
        case 6:
            switch (Name)
            {
                case 'Q':
                    player[1].Block_num = Data;
                    break;
                case 'A':
                    player[2].Block_num = Data;
                    break;
                case 'S':
                    player[3].Block_num = Data;
                    break;
                case 'J':
                    player[4].Block_num = Data;
                    break;
                default:
                    break;
            }
            break;
        case 7:
            switch (Name)
            {
                case 'Q':
                    player[1].Robot_num = Data;
                    break;
                case 'A':
                    player[2].Robot_num = Data;
                    break;
                case 'S':
                    player[3].Robot_num = Data;
                    break;
                case 'J':
                    player[4].Robot_num = Data;
                    break;
                default:
                    break;
            }
            break;
        case 8:
            switch (Name)
            {
                case 'Q':
                    player[1].god = Data;
                    break;
                case 'A':
                    player[2].god = Data;
                    break;
                case 'S':
                    player[3].god = Data;
                    break;
                case 'J':
                    player[4].god = Data;
                    break;
                default:
                    break;
            }
            break;
    }
}

//数字char转int，正数
// strtol
int charToInt(char *charNum)
{
    if (charNum[0] == '-') {
        return -charToInt(&charNum[1]);
    }
    int intNum = 0;
    int i = 0;
    while (charNum[i])
    {
        intNum *= 10;
        intNum += (charNum[i] - '0');
        i++;
    }
    return intNum;
}

//测试输出，输入当前玩家在order中的索引
void PrintState(int n)
{
    FILE *out;
    out = fopen("out_test.out", "w");
    const char name[5] = "QASJ";
    fprintf(out, "seq ");
    for (int i = n; i < playerNumber; i++)
    {
        if (!player[order[i]].Bust) {
            fprintf(out, "%c", name[order[i] - 1]);
        }
    }
    for (int i = 0; i < n; i++)
    {
        if (!player[order[i]].Bust) {
            fprintf(out, "%c", name[order[i] - 1]);
        }
    }
    fprintf(out, "\n");

    bool isInGame[5] = {false};
    for (int i = 0; i < playerNumber; i++)
    {
        if (!player[order[i]].Bust)
        {
            isInGame[order[i]] = true;
        }
    }
    for (int i = 1; i <= 4; i++)
    {
        if (isInGame[i])
        {
            fprintf(out, "%c %d %ld %d %d %d %d %d %d\n", name[i - 1], player[i].Position, player[i].money, player[i].credit, -player[i].state, player[i].Bomb_num, player[i].Block_num, player[i].Robot_num, player[i].god);
        }
    }
    for (int i = 0; i < 70; i++)
    {
        if (place[i].Owner >= 1 && place[i].Owner <= 4 || place[i].Tool != 0)
        {
            if (place[i].Owner > 4 || place[i].Owner == 0)
            {
                fprintf(out, "building %d %d 0 %d\n", i, place[i].level, place[i].Tool);
            }
            else
            {
                fprintf(out, "building %d %d %c %d\n", i, place[i].level, name[place[i].Owner - 1], place[i].Tool);
            }
        }
    }
    fclose(out);
}

//主函数
int main()
{
    // system("color f0");//f-white background,0-black word
    init();
    printMap();
    cout << "请设置玩家初始资金（范围1000~50000）：";
    cin.clear();
    cin.sync();
    cin >> startMoney;
    for (int i = 1; i <= 4; i++)
    {
        player[i].money = startMoney;
    }
    do{
        cout << "请选择2~4位不重复玩家，输入编号即可（1、钱夫人；2、阿土伯；3、孙小美；4、金贝贝）：";
        cin.clear();
        cin.sync();
        cin >> choosePlayer;
    } while (!IsValid(choosePlayer));
    for (int i = 0; i < 4; i++)
    {
        switch (choosePlayer[i])
        {
            case '1':
                order[i] = 1;
                player[1].Position = 0;
                playerNumber++;
                break;
            case '2':
                order[i] = 2;
                player[2].Position = 0;
                playerNumber++;
                break;
            case '3':
                order[i] = 3;
                player[3].Position = 0;
                playerNumber++;
                break;
            case '4':
                order[i] = 4;
                player[4].Position = 0;
                playerNumber++;
                break;
            default:
                break;
        }
    }
    aliveNumber = playerNumber;
    //大循环
    for (int i = 0;; i++)
    {
        printMap();
        if (i == playerNumber)
        {
            i = 0;
        }
        nowPlayer = order[i];
        nowPosition = player[nowPlayer].Position;
        if (player[nowPlayer].Bust)
        { // dead
            continue;
        }
        setColor(nowPlayer);
        if (player[nowPlayer].state < 0)
        { // ban
            player[nowPlayer].state++;

            // fine and mine
            if (place[nowPosition].Owner >= 1 && place[nowPosition].Owner <= 4 && place[nowPosition].Owner != nowPlayer) {
                fine();
            } else if (place[nowPosition].Owner == 5) {
                cout << "您走到了矿地，获得" << place[nowPosition].count << "点数" << endl;
                player[nowPlayer].credit += place[nowPosition].count;
            }

            if (player[nowPlayer].god > 0)
                player[nowPlayer].god--;
            continue;
        }
        // Roll之前
        while (true)
        {
            setColor(nowPlayer);
            cout << player[nowPlayer].Name << ">";
            // setColor(0);
            getchar();
            cin.clear();
            cin.sync();
            command_separate();   //此处将命令检索到二维数组中
            int state_command =command_deal(i); //根据命令调用各个函数
            if (state_command==-1) continue;
            if (state_command== 1) break;
        }

        //获胜判定
        if (aliveNumber == 1)
        {
            cout << "游戏结束！";
            for (int check = 0; check < playerNumber; check++)
            {
                if (!player[order[check]].Bust)
                {
                    PrintState(check);
                    cout << player[order[check]].Name << "获胜！！" << endl;
                    return 0;
                }
            }
        }
        // command清理
        int clear = 0;
        while (command[clear])
            command[clear++] = 0;
    } //大循环
}

int command_deal(int people)
{
    if (!strcmp(commands[0], "help") && (num_word == 1))
    {
        help_operations();
    }
    else if (!strcmp(commands[0], "quit") && (num_word == 1))
    {
        cout << "-强制退出-" << endl;
        exit(0);
    }
    else if (!strcmp(commands[0], "roll") && (num_word == 1))
    {
        int rollResult = Roll();
        cout << rollResult << endl;
        cout << "您投掷的点数为："<<rollResult << endl;
        Walk(rollResult);
        return 1;
    }
    else if (!strcmp(commands[0], "robot") && (num_word == 1))
    {
        if (player[nowPlayer].Robot_num == 0)
        {
            cout << "对不起，您没有机器娃娃道具！！" << endl;
            return -1;
        }
        player[nowPlayer].Robot_num--;
        int checkToolPlace = nowPosition + 1;
        for (int checkTool = 0; checkTool < 10; checkTool++)
        {
            if (checkToolPlace == 70)
            {
                checkToolPlace = 0;
            }
            place[checkToolPlace].Tool = 0;
            checkToolPlace++;
        }
    }
    else if (!strcmp(commands[0], "query") && (num_word == 1))
    {
        bool Exist = false;
        cout << "Money:" << player[nowPlayer].money << "   Credit:" << player[nowPlayer].credit << endl;
        cout << "Position\tLevel\tPrice" << endl;
        for (int searchOwner = 1; searchOwner < 65; searchOwner++)
        {
            if (place[searchOwner].Owner == nowPlayer)
            {
                Exist = true;
                cout << searchOwner << "\t\t" << place[searchOwner].level << "\t" << place[searchOwner].price << endl;
            }
        }
        if (Exist == 0)
        {
            cout << "您尚未投资房产。" << endl;
        }
    }
    else if (!strcmp(commands[0], "print") && (num_word == 1))
    {
        PrintState(people);
    }
    else if (!strcmp(commands[0], "step") && (num_word == 2))
    {
        int stepNumber = char2int(commands[1]);
        if(!stepNumber){
            cout << "非法命令:"<<commands[0]<<' '<<commands[1]<<"\n请重新输入" << endl;
            return -1;
        }
        Walk(stepNumber);
        return 1;
    }
    else if (!strcmp(commands[0], "sell") && (num_word == 2))
    {
        int sellPosition = char2int(commands[1]);
        if(sellPosition==0){
            cout << "非法命令:"<<commands[0]<<' '<<commands[1]<<"\n请重新输入" << endl;
            return -1;
        }
        if (place[sellPosition].Owner != nowPlayer)
        {
            cout << "不可售卖！！" << endl;
        }
        else
        {
            place[sellPosition].Owner = 0;
            player[nowPlayer].money += 2 * (place[sellPosition].level + 1) * place[sellPosition].price;
            printMap();
        }
    }
        // else if(!strcmp(commands[0],"bomb")&&(num_word==2)){

        // }
    else if (!strcmp(commands[0], "block") && (num_word == 2))
    {
        if (player[nowPlayer].Block_num == 0)
        {
            cout << "对不起，您没有路障道具！！" << endl;
            return -1;
        }
        int pos = char2int(commands[1]);
        if(!pos || pos < -10 || pos > 10){
            cout << "非法命令:"<<commands[0]<<' '<<commands[1]<<"\n请重新输入" << endl;
            return -1;
        }
        int absolutePosition = set_block(pos);
        if (absolutePosition == -1)
        {
            return -1;
        }
        player[nowPlayer].Block_num--;
        place[absolutePosition].Tool = 2;
        printMap();
    }
    else if (!strcmp(commands[0], "preset"))
    {
        // TestPart();
        if(preset_deal()==-1){
            return -1;
        }
        nowPosition = player[nowPlayer].Position;
    }
    else
    {
        cout << "wrong command" << endl;
    }
    return 0;
}

int command_separate()
{
    char temp[50] = {'\0'};
    cin.get(temp, 50);
    // cout<<"temp为"<<temp<<endl;
    for (int x = 0; x < 50; x++)
    { //统一小写
        if (temp[x] >= 'A' && temp[x] <= 'Z')
        {
            temp[x] += 32;
        }
    }
    strcpy(command,temp);
    num_word = 0;
    int k = 0, j = 0;
    int state = 0;
    memset(commands, 0, sizeof(char) * 400);
    while (temp[k])
    {
        if (temp[k] == ' ')
        {
            if (state)
            {
                num_word++;
            }
            state = 0;
            j = 0;
            k++;
            continue;
        }
        state = 1;
        commands[num_word][j] = temp[k];
        j++;
        k++;
    }
    num_word++;
    return 0;
}

void help_operations()
{
    cout << "*** usage: COMMAND [argument1] [argument2] … ***" << endl;
    cout << "Supported COMMANDs:" << endl;
    cout << "\tRoll: roll the dice, steps randomly generated between 1 and 6" << endl;
    cout << "\tSell <num>: sell assets at position <num>" << endl;
    cout << "\tQuery: check personal info" << endl;
    cout << "\tBlock <num>: set road block at relative position between -10 and 10" << endl;
    cout << "\tRobot: use robot to clear 10 zones ahead" << endl;
    cout << "\tQuit: exit the game" << endl;
    cout << "\tHelp: print this message" << endl;
    cout << endl;
    cout << "*** debug usage: DEBUG_COMMAND [argument1] [argument2] … ***" << endl;
    cout << "Supported DEBUG_COMMANDs:" << endl;
    cout << "\tStep <num>: move forward <num> steps" << endl;
    cout << "\tPreset …: special command to modify game variables" << endl;
    cout << "\tPrint: print all states except for default ones" << endl;
}

int set_block(int relativePosition)
{
    int absolutePosition = nowPosition + relativePosition;
    if (absolutePosition < 0)
    {
        absolutePosition += 70;
    }
    else if (absolutePosition >= 70)
    {
        absolutePosition -= 70;
    }
    if (absolutePosition == 14 || absolutePosition == 49) {
        cout << "公园不可放置道具！" << endl;
        return -1;
    }
    if (place[absolutePosition].Tool != 0) {
        cout << "已有道具，不可放置！" << endl;
        return -1;
    }
    //有玩家的位置不能放道具
    for (int checkPlayerPosition = 0; checkPlayerPosition < playerNumber; checkPlayerPosition++)
    {
        if (!player[order[checkPlayerPosition]].Bust)
        {
            if (player[order[checkPlayerPosition]].Position == absolutePosition)
            {
                cout << "有玩家的位置不可放置道具！！" << endl;
                return -1;
            }
        }
    }
    return absolutePosition;
}

int char2int(char* input){
    int len = strlen(input);
    bool allnum = true;
    for (int i = 0; i < len; i++) {
        if (i == 0 && input[i] == '-') {
            continue;
        }
        allnum = input[i] >= '0' && input[i] <= '9';
        if (!allnum) {
            break;
        }
    }
    return (allnum)?atoi(input):-11;
}

int preset_deal(){
    if(!strcmp(commands[1], "seq")&& (num_word == 3)){
        playerNumber = 0;
        if (command[11] != 0)
        {
            order[0] = setSeq(command[11]);
            playerNumber++;
            nowPlayer = order[0];
        }
        if (command[12] != 0)
        {
            order[1] = setSeq(command[12]);
            playerNumber++;
        }
        if (command[13] != 0)
        {
            order[2] = setSeq(command[13]);
            playerNumber++;
        }
        if (command[14] != 0)
        {
            order[3] = setSeq(command[14]);
            playerNumber++;
        }
        aliveNumber = playerNumber;
    }
    else {
        int data = char2int(commands[3]);
        if (data <= -12) {
            cout << "非法命令" << "\n请重新输入" << endl;
            return -1;
        }
        if (!strcmp(commands[1], "pos") && (num_word == 4)) {
            setNameColumnData(commands[2][0], 1, char2int(commands[3]));
            printMap();
        } else if (!strcmp(commands[1], "money") && (num_word == 4)) {
            setNameColumnData(commands[2][0], 2, char2int(commands[3]));
        } else if (!strcmp(commands[1], "credit") && (num_word == 4)) {
            setNameColumnData(commands[2][0], 3, char2int(commands[3]));
        } else if (!strcmp(commands[1], "state") && (num_word == 4)) {
            setNameColumnData(commands[2][0], 4, char2int(commands[3]));
        } else if (!strcmp(commands[1], "barrier") && (num_word == 4)) {
            setNameColumnData(commands[2][0], 6, char2int(commands[3]));
        } else if (!strcmp(commands[1], "robot") && (num_word == 4)) {
            setNameColumnData(commands[2][0], 7, char2int(commands[3]));
        } else if (!strcmp(commands[1], "god") && (num_word == 4)) {
            setNameColumnData(commands[2][0], 8, char2int(commands[3]));
        } else if (!strcmp(commands[1], "building") && (num_word == 5)) {
            place[char2int(commands[2])].level = commands[3][0] - '0';
            place[char2int(commands[2])].Owner = setSeq(commands[4][0]);
            printMap();
        } else if (!strcmp(commands[1], "building-tool") && (num_word == 4)) {
            place[char2int(commands[2])].Tool = commands[3][0] - '0';
            printMap();
        } else {
            cout << "非法命令" << "\n请重新输入" << endl;
            return -1;
        }
    }
    return 0;
}